package com.tx.service.impl;

import com.tx.dao.UserDao;
import com.tx.dao.impl.UserDaoImpl;
import com.tx.entity.User;
import com.tx.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String account, String password) {
        return userDao.findByAccountAndPassword(account, password);
    }

    @Override
    public boolean addUser(String username, String account, String password, int permission) {
        User user = new User();
        user.setUsername(username);
        user.setAccount(account);
        user.setPassword(password);
        user.setPermission(permission);
        int result = userDao.add(user);
        return result > 0;
    }

    @Override
    public boolean updateUser(int id, String username, String account, String password, int permission) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setAccount(account);
        user.setPassword(password);
        user.setPermission(permission);
        int result = userDao.update(user);
        return result > 0;
    }

    @Override
    public boolean deleteUser(int id) {
        int result = userDao.delete(id);
        return result > 0;
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.findAll();
    }

    @Override
    public List<User> getUsersByUsername(String username) {
        return userDao.findByUsername(username);
    }
}